package ucm;

import java.sql.Connection;
import java.sql.Statement;
import model.Database_Model;
import model.User_Model;

public class UCDeleteUser {

    private String name, psw;
    private User_Model user;

    public UCDeleteUser(view.Air_Delete del) {
        this.user = del.getUserModel();

        this.name = user.getUsername();
        this.psw = user.getPassword();
    }

    public String run() {
        boolean inserted = false;

        if (this.name.isEmpty()) {
            return "Please input your name in the list.";
        } else if (this.psw.isEmpty()) {
            return "Please input your password in the list.";
        } else if (name.length() > 15 || psw.length() > 15) {
            return "Please input user name or password by correct form.";
        } else {
            if (new ucm.UCCheckUser(name).run().equalsIgnoreCase("The user name already exists.")) {
                try {
                    Connection con = new Database_Model().getConnection();
                    Statement stmt = con.createStatement();
                    stmt.executeUpdate("delete from user where user.Name = '" + name + "'");
                    return "Congratulate! ";
                } catch (Exception e) {
                    return "Failed! ";
                }
            } else {
                return "The user name have not been used.";
            }
        }
    }
}
